package com.example.petstore.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.User;
import com.example.petstore.mapper.UserMapper;
import com.example.petstore.util.ALIYUNCodeUtil;
import com.example.petstore.util.GiteeLogin;
import com.example.petstore.util.ZFBqrUtil;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class test {

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/test")
    public String test(){
//        System.out.println(LocalDateTime.now().toLocalDate());
//        log.info("111");
//        log.debug("222");
//        System.out.println("iii");
        return "common/chat";
    }

    @RequestMapping("/testcode")
    public String testcode(){
        return "test_code";
    }

    @RequestMapping("/testzfb")
    public String testzfb(){
        return "test_zfb";
    }

    @RequestMapping("/sendPhoneCode")
    @ResponseBody
    public CommonResponse<Object>  sendPhoneCode(@RequestParam("phoneNumber") String phoneNumber){
        System.out.println(phoneNumber);
        String code = ALIYUNCodeUtil.sendCode(phoneNumber);
        if(code == null){
//            发送失败
            return CommonResponse.creatForError("发送失败");
        }else{
//            todo:把code存进数据库
            return CommonResponse.creatForSuccessMessage("发送成功");
        }

    }




    @GetMapping("/common/kaptcha")/*需要保留*/
    public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        byte[] captchaOutputStream = null;
        ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
        try {
            //生产验证码字符串并保存到session中
            String verifyCode = captchaProducer.createText();
            httpServletRequest.getSession().setAttribute("verifyCode", verifyCode);
            System.out.println(verifyCode);
            BufferedImage challenge = captchaProducer.createImage(verifyCode);
            ImageIO.write(challenge, "jpg", imgOutputStream);
        } catch (IllegalArgumentException e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        captchaOutputStream = imgOutputStream.toByteArray();
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setHeader("Pragma", "no-cache");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/jpeg");
        ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
        responseOutputStream.write(captchaOutputStream);
        responseOutputStream.flush();
        responseOutputStream.close();
    }

    @PostMapping("/loginh")
    public String loginByKaptcha(@RequestParam("verifyCode") String verifyCode,
                                 HttpSession session){
        String kaptchaCode = session.getAttribute("verifyCode") + "";
        if(verifyCode.equals(kaptchaCode)){
            return "test_success";
        }

        return "test_index";
    }

    @PostMapping("/getList")
    @ResponseBody
    public CommonResponse<String> getUsers(){
//        User user1 = new User();
//        user1.setUsername("111");
//        User user2 = new User();
//        user2.setUsername("222");
//        List<User> users = new ArrayList<>();
//        users.add(user1);
//        users.add(user2);
//        return CommonResponse.creatForSuccess(users);
//        Map<String, Object> map = new HashMap<>();
//        map.put("totalIncome", 1111.2);
//        map.put("totalOutcome", 2222.2);
//        map.put("totalOutcome", 2222.2);
//        Map<Integer, Object> map1 = new HashMap<>();
//        map1.put(1, 1.1);
//        map1.put(2, 2.2);
//        map.put("incomesByList", map1);
        String code = "1111";
        return CommonResponse.creatForSuccessMessage("发送成功");
    }


    @GetMapping("/testchanngephone")
    @ResponseBody
    public CommonResponse test111(){
        String username = "wjh";
        String phoneNumber = "18655596813";
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("phonenumber", phoneNumber);
        updateWrapper.eq("username", username);
        updateWrapper.set("password", "2222");
        int result = userMapper.update(updateWrapper);
        System.out.println(result);
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("username", username);
        User user = userMapper.selectOne(queryWrapper1);
        System.out.println(user);
        return CommonResponse.creatForSuccess();
    }
}
